package kuaidi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Query 
{
	private static  String order(String company,String orderNo){
		//String url = "http://www.kuaidi100.com/query?type=yunda&postid=1&id=1&valicode=&temp=0.3015635129995644";  
		String url = "http://www.kuaidi100.com/query?type="+company+"&postid="+orderNo+"&id=1&valicode=&temp=0.3015635129995644";  
        try {  
            HttpURLConnection.setFollowRedirects(true);  
            HttpURLConnection http = (HttpURLConnection) (new URL(url).openConnection());  
            http.setDoOutput(true);  
            http.setDoOutput(true);  
            http.setInstanceFollowRedirects(true);  
            http.setRequestMethod("GET");  
            http.setRequestProperty("Connection", "keep-alive");  
            http.setRequestProperty("X-Requested-With", "XMLHttpRequest");  
            http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER");  
            http.setRequestProperty("Accept", "*/*");  
            http.setRequestProperty("Referer", "http://www.kuaidi100.com/");  
            http.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");  
            http.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");  
            http.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");  
              
//            System.out.println("response  is : "+http.getResponseCode()+" "+http.getResponseMessage());  
            String contentEncoding = http.getContentEncoding();  
//            System.out.println("response  encoding is : "+contentEncoding);  
            InputStream  in = null;  
            if("gzip".equalsIgnoreCase(contentEncoding)){  
                in = new GZIPInputStream(http.getInputStream());   
            }else{  
                in = http.getInputStream();  
            }  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            int data = -1;  
            while((data = in.read())!=-1){  
                baos.write(data);  
            }  
            String resp = baos.toString("utf8");  
            return resp;
//            System.out.println(resp);  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return null;
	}
	
	public static KOrderInfo get(String company,String orderNo){
		Gson gson = new Gson();
		String json =order(company,orderNo);
		KOrderInfo uu = gson.fromJson(json, new TypeToken<KOrderInfo>() {}.getType());
		if(uu!=null){
			return uu;
		}else{
			return null;
		}
	}
	
    public static void main1111( String[] args )
    {
    	KOrderInfo aa = Query.get("yunda", "1900330840346");
    	if(aa!=null){
			System.out.println("ok"+aa.getNu());
		}else{
			System.out.println("error");
		}
    }
}
